package org.wk.common.jpush;

import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wk.common.jpush.vo.*;

import java.util.List;
import java.util.Map;

/**
 * @author df.wu
 * @version V1.0
 * @title: PushService
 * @package com.athena.base.service.push
 * @Date 16:21 2017/11/27
 * @description:
 */
@Service("pushService")
public class PushService {
    private static Logger logger = LoggerFactory.getLogger(PushService.class);

    @Autowired
    private ClientPushService clientPushService;

    /**
     * 推送给所有人通知
     * @param alert 标题
     * @param extras 扩展字段
     */
    public void pushNotification(String alert, Map<String,String> extras){
        AndroidNotification androidNotification = AndroidNotification.newBuilder().setAlert(alert).addExtras(extras).build();
        IosNotification iosNotification = IosNotification.newBuilder().setAlert(alert).addExtras(extras).build();
        Notification notification = Notification.newBuilder().addPlatformNotification(androidNotification).addPlatformNotification(iosNotification).build();
        pushNotification(Platform.all(), Audience.all(), notification);
    }

    /**
     * 按别名推送通知
     * @param alert 标题
     * @param extras 扩展字段
     * @param alias 别名
     */
    public void pushNotification(String alert, Map<String,String> extras, String... alias){
        AndroidNotification androidNotification = AndroidNotification.newBuilder().setAlert(alert).addExtras(extras).build();
        IosNotification iosNotification = IosNotification.newBuilder().setAlert(alert).addExtras(extras).build();
        Notification notification = Notification.newBuilder().addPlatformNotification(androidNotification).addPlatformNotification(iosNotification).build();
        pushNotification(Platform.all(), Audience.alias(alias), notification);
    }
    /**
     * 按别名推送通知
     * @param alert 标题
     * @param extras 扩展字段
     * @param alias 别名
     */
    public void pushNotification(String alert, Map<String,String> extras, List<String> alias){
        AndroidNotification androidNotification = AndroidNotification.newBuilder().setAlert(alert).addExtras(extras).build();
        IosNotification iosNotification = IosNotification.newBuilder().setAlert(alert).addExtras(extras).build();
        Notification notification = Notification.newBuilder().addPlatformNotification(androidNotification).addPlatformNotification(iosNotification).build();
        pushNotification(Platform.all(), Audience.alias(alias), notification);
    }


    /**
     * 向所有用户发送自定义消息
     * @param extras
     */
    public void pushMessage(Map<String,String> extras){
        Message message = Message.newBuilder().addExtras(extras).build();
        pushMessage(Platform.all(), Audience.all(), message);
    }

    /**
     * 向所有用户发送自定义消息
     * @param content_type 内容类型
     * @param msg_content 内容
     */
    public void pushMessage(String content_type, String msg_content){
        Message message = Message.newBuilder().setContentType(content_type).setMsgContent(msg_content).build();
        pushMessage(Platform.all(), Audience.all(), message);
    }


    /**
     * 按别名发送自定义消息
     * @param content_type 内容类型
     * @param msg_content 内容
     * @param alias 别名
     */
    public void pushMessage(String content_type, String msg_content, String... alias){
        Message message = Message.newBuilder().setContentType(content_type).setMsgContent(msg_content).build();
        pushMessage(Platform.all(), Audience.alias(alias), message);
    }
    /**
     * 按别名 推送自定义消息
     * @param content_type 内容类型
     * @param msg_content 内容
     * @param alias 别名
     */
    public void pushMessage(String content_type, String msg_content, List<String> alias){
        Message message = Message.newBuilder().setContentType(content_type).setMsgContent(msg_content).build();
        pushMessage(Platform.all(), Audience.alias(alias), message);
    }

    /**
     * 按别名 发送自定义消息
     * @param extras 扩展字段
     * @param alias 别名
     */
    public void pushMessage(Map<String,String> extras, String... alias){
        Message message = Message.newBuilder().addExtras(extras).build();
        pushMessage(Platform.all(), Audience.alias(alias), message);
    }
    /**
     * 按别名 发送自定义消息
     * @param extras 扩展字段
     * @param alias 别名
     */
    public void pushMessage(Map<String,String> extras, List<String> alias){
        Message message = Message.newBuilder().addExtras(extras).build();
        pushMessage(Platform.all(), Audience.alias(alias), message);
    }


    /**
     * 推送给所有APP 通知
     * @param customPlatformEnum 平台
     * @param audience 目标
     * @param customNotification 通知
     */
    private void pushNotification(Platform customPlatformEnum, Audience audience, Notification customNotification){
        push(customPlatformEnum, audience, customNotification, null);
    }

    /**
     * 推送给所有APP 消息
     * @param customPlatformEnum 平台
     * @param audience 目标
     * @param message 消息
     */
    private void pushMessage(Platform customPlatformEnum, Audience audience, Message message){
        push(customPlatformEnum, audience, null, message);
    }

    /**
     * 基础推送方法
     * @param customPlatformEnum  平台
     * @param audience  目标
     * @param customNotification 通知
     * @param message 消息
     */
    private void push(Platform customPlatformEnum, Audience audience, Notification customNotification, Message message){
        PushPayload pushPayload = PushPayload.newBuilder().setPlatform(customPlatformEnum)
                                                          .setAudience(audience)
                                                          .setNotification(customNotification)
                                                          .setMessage(message).build();


//        CustomPush customPush = new CustomPush(customPlatformEnum.getCode(), audience, customNotification, message);
        String pushString = pushPayload.toString();
        System.out.printf(pushString);
        clientPushService.pushString(pushPayload);
    }



}
